#-*- coding: utf-8 -*-
'''
Created on Aug 27, 2012

@author: bbg
'''

from sqlalchemy import Integer, Column, String, MetaData, Table
from sqlalchemy.orm import mapper
    
class ConfigUnitRelation(object):

    _Mapper_ = None
    _company_id_ = -1
    _shop_id_ = -1    
    def __init__(self):
        self.id = 0
        self.inner_rank_id = 0
        self.dish_id = 0
        self.unit = ''
        self.unit_price = 0
        self.price_type = 0
        self.cost_price = 0
        self.deduct = 0
        
    @staticmethod
    def doMapping(company_id, shop_id):
        '''
        按company_id分库，shop_id对应公司分店，每次生成对象前都要执行一次，以便map到正确的数据库上去
        '''
        if ConfigUnitRelation._Mapper_ is not None:
            ConfigUnitRelation._Mapper_.dispose()
            
        metaData = MetaData()
        configUnitRelationTable = Table('config_unit_relation', metaData,
                           Column('id', Integer, primary_key = True),
                           Column('inner_rank_id', Integer, primary_key = True),
                           Column('dish_id', Integer, primary_key = True),
                           Column('unit', String),
                           Column('unit_price', Integer),
                           Column('price_type', Integer),
                           Column('cost_price', Integer),
                           Column('deduct', Integer),
                           schema = "shop_%s_%s" % (company_id, shop_id))
        
        ConfigUnitRelation._Mapper_ = mapper(ConfigUnitRelation, configUnitRelationTable)
    